﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

public class Ban1Throwable : Throwable
{
    //爆炸效果的特效.
    public GameObject exploredEffect;
    //被复制出来的爆炸特效.
    GameObject explosionGo;

    //当被抓起来的物体，被扔出去的瞬间，会执行此函数.
    protected override void OnDetachedFromHand(Hand hand)
    {
        //执行父类的同名函数的代码.
        base.OnDetachedFromHand(hand);
        //3秒后，爆炸.
        Invoke("Explored", 3f);
    }

    //爆炸函数.
    void Explored()
    {
        //复制爆炸效果的特效.
        explosionGo = Instantiate(exploredEffect,transform.position,Quaternion.identity) as GameObject;
        Invoke("DestroyEffect", 3f);

        int layerMask = LayerMask.NameToLayer("Enemy");
        layerMask = 1 << layerMask;
        //检测以方块为中心，以4米为半径，范围内的所有的Layer为Enemy的具有碰撞器的物体.
        Collider[] colArray = Physics.OverlapSphere(transform.position, 4, layerMask);
        if (colArray != null && colArray.Length>0)
        {
            for(int i=0;i< colArray.Length;i++)
            {
                Ban1Enemy enemy = colArray[i].GetComponent<Ban1Enemy>();
                if(enemy!=null)
                {
                    enemy.OnHurt();
                }
            }
        }
    }

    //删除复制出来的爆炸特效物体.
    void DestroyEffect()
    {
        Destroy(explosionGo);
    }
}
